//
//  WalletSettingVC.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/5/28.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "WalletSettingVC.h"

@interface WalletSettingVC () {
    NSString *address;
}


@property(weak, nonatomic) IBOutlet UIButton *backBt;
@property(weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property(strong, nonatomic) UILabel     *addressLb;
@property(strong, nonatomic) UIImageView *headerIv;
@property(strong, nonatomic) UILabel     *phoneLb;
@property(strong, nonatomic) UILabel     *versionLb;

@end

@implementation WalletSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView {
    [[self.backBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [self updateView];
}

- (void)updateView {
    for (NSInteger i=self.scrollView.subviews.count-1; i>=0; i--) {
        [self.scrollView.subviews[i] removeFromSuperview];
    }
    address = getValueByKey(@"ss_wallet_address");
    float width = [UIScreen mainScreen].bounds.size.width-72;
    float height = width*322/561;
    float originY = 0;
    self.headerIv = [[UIImageView alloc] initWithFrame:CGRectMake(0, originY, width, height)];
    [self.headerIv setImage:[UIImage imageNamed:@"img_card_unbinding"]];
    [self.scrollView addSubview:self.headerIv];
    
    self.phoneLb = [[UILabel alloc] initWithFrame:CGRectMake(0, height*3/4, width, 20)];
    self.phoneLb.text = @"182****250";
    self.phoneLb.textColor = [UIColor whiteColor];
    self.phoneLb.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:self.phoneLb];
    
    originY +=height;
    float margin1 = 9;
    float margin2 = 4;
    float h = 45;
    float topMargin[] = {margin1, margin2, margin2, margin2};
    
    void (^block) (void) = ^ {
        NSString *msg = @"\n\n1、开启Accel加速链挖矿后，您可以通过分享闲置带宽、存储空间及计算资源来获取积分。\n2、昨日获得的积分于每日凌晨2:00更新，积分将自动存入你的账户余额中。\n3、绑定钱包后，所有设备收益将自动转入绑定的钱包地址，但账户余额不会自动转入。\n\n";
        UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"挖矿活动说明" message:msg preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [vc addAction:cancel];
        [self setUIAlertController:vc MessageAlign:NSTextAlignmentJustified];
        [self presentViewController:vc animated:YES completion:nil];
    };
    
//    NSString *titles[] = {@"修改钱包地址",@"解除地址绑定",@"说明",@"用户协议"};
//    NSString *titles2[] = {@"绑定钱包地址",@"说明",@"用户协议"};
    
    NSString *titles[] = {@"修改钱包地址",@"解除地址绑定",@"说明"};
    NSString *titles2[] = {@"绑定钱包地址",@"说明"};
    int count = address ? 3 : 2;
    for (int i=0; i<count; i++) {
        originY += topMargin[i];
        UIButton *bt = [[UIButton alloc] initWithFrame:CGRectMake(0, originY, width, h)];
        [bt setBackgroundColor:[UIColor whiteColor]];
        bt.layer.cornerRadius = globalRadius;
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, h)];
        lb.text = count==4 ? titles[i] : titles2[i];
        [lb setFont:[UIFont systemFontOfSize:14]];
        lb.textColor = colorWithString(@"#4f525b");
        [bt addSubview:lb];
        if (i==0) {
            self.addressLb = lb;
        }
        float imgH = 12;
        float imgW = imgH*20/42;
        UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(width-imgW-10, h/2-imgH/2, imgW, imgH)];
        [iv setImage:[UIImage imageNamed:@"img_jt_1"]];
        [bt addSubview:iv];
        [[bt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
            switch (i) {
                case 0: {
                    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
                    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"WalletBindVCID"];
                    [self.navigationController pushViewController:vc animated:YES];
                    break;
                }
                case 1: {
                    if (count == 4) {
                        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
                        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"WalletUnbindVCID"];
                        [self.navigationController pushViewController:vc animated:YES];
                    } else {
                        block();
                    }
                    break;
                }
                case 2: {
                    if (count == 3) {
                        block();
                    } else {
                        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
                        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ProtocolVCID"];
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                    break;
                }
                case 3: {
                    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"User" bundle:nil];
                    UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"ProtocolVCID"];
                    [self.navigationController pushViewController:vc animated:YES];
                    break;
                }
                default:
                    break;
            }
        }];
        [self.scrollView addSubview:bt];
        originY += h;
    }
    [self.scrollView setContentSize:CGSizeMake(0, originY)];
    
    if (address) {
        self.addressLb.text = @"修改收益地址";
        [self.headerIv setImage:[UIImage imageNamed:@"img_card_binding"]];
        address = [address substringFromIndex:address.length-4];
        self.phoneLb.text = [NSString stringWithFormat:@"**** **** **** **** %@", address];
    } else {
        self.addressLb.text = @"绑定收益地址";
        [self.headerIv setImage:[UIImage imageNamed:@"img_card_unbinding"]];
        NSString *phone = [UserManager shareInstance].user.phone;
        if (phone && phone.length>=11) {
            phone = [phone stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        }
        self.phoneLb.text = phone;
    }
}

- (void)requestData {
    [[[HttpRequest shareInstance] signGet:s_wallet map:nil] subscribeNext:^(id x) {
        if ([x[@"code"] intValue] == 200) {
            self->address = x[@"data"];
            saveValueByKey(self->address, @"ss_wallet_address");
            [self updateView];
        } else {
            if ([x[@"code"] intValue] == 1005) {
                self->address = nil;
                removeValueByKey(@"ss_wallet_address");
                [self updateView];
            }
        }
    } error:^(NSError *error) {

    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end
